package com.edu.framework.websocket.handler;

import com.alibaba.fastjson2.JSONObject;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.weather.WeatherRequest;
import com.edu.framework.websocket.message.weather.Coordinate;
import com.edu.framework.websocket.service.PushService;
import com.edu.framework.websocket.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.Map;

@Component
public class WeatherRequestHandler implements IHandler<WeatherRequest> {

    @Autowired
    private PushService pushService;

    @Override
    public void execute(WebSocketSession session, WeatherRequest message) {
        if (message == null) {
            return;
        }

        Response<JSONObject> response = pushService.getWeather(message);
        try {
            WebSocketHolder.send(session, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getType() {
        return WeatherRequest.TYPE;
    }
}
